package com.itheima.service.db.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.domain.SmsHomeNewProduct;
import com.itheima.mapper.SmsHomeNewProductMapper;
import com.itheima.service.db.SmsHomeNewProductService;
import com.itheima.vo.PageBeanVo;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

@DubboService
public class SmsHomeNewProductServiceImpl implements SmsHomeNewProductService {

    @Autowired
    SmsHomeNewProductMapper smsHomeNewProductMapper;

    /**
     * 营销-新品推荐-分页条件查询
     * @param pageNum
     * @param pageSize
     * @param productName
     * @param recommendStatus
     * @return
     */
    @Override
    public PageBeanVo findByPage(Integer pageNum, Integer pageSize, String productName, Integer recommendStatus) {
        //获取全部分页数据
        Page<SmsHomeNewProduct> page = new Page<>(pageNum,pageSize);

        //创建条件构造器
        LambdaQueryWrapper<SmsHomeNewProduct> qw = new LambdaQueryWrapper<>();
        qw.like(StrUtil.isNotEmpty(productName),SmsHomeNewProduct::getProductName,productName)
                .eq(recommendStatus != null,SmsHomeNewProduct::getRecommendStatus,recommendStatus)
                .orderByDesc(SmsHomeNewProduct::getSort);

        //根据mapper查询条件分页数据
        Page<SmsHomeNewProduct> smsHomeNewProductPage = smsHomeNewProductMapper.selectPage(page, qw);

        //返回分页对象pageBeanVo;
        return new PageBeanVo(pageNum,pageSize, smsHomeNewProductPage.getTotal(),smsHomeNewProductPage.getRecords());
    }

    /**
     * 营销-新品-修改首页新品单个|多个推荐状态
     * @param id
     * @param recommendStatus
     */
    @Override
    public void update(Long id, Integer recommendStatus) {
        SmsHomeNewProduct smsHomeNewProduct = smsHomeNewProductMapper.selectById(id);
        smsHomeNewProduct.setRecommendStatus(recommendStatus);
        smsHomeNewProductMapper.updateById(smsHomeNewProduct);
    }

    /**
     * 营销-新品-删除新品推荐
     * @param id
     */
    @Override
    public void delete(Long id) {
        smsHomeNewProductMapper.deleteById(id);
    }

    /**
     * 营销-新品-修改首页新品排序
     * @param id
     * @param sort
     */
    @Override
    public void updateSort(Long id, Integer sort) {
        SmsHomeNewProduct smsHomeNewProduct = smsHomeNewProductMapper.selectById(id);
        smsHomeNewProduct.setSort(sort);
        smsHomeNewProductMapper.updateById(smsHomeNewProduct);
    }

    /**
     * 营销-新品-添加首页新品
     * @param smsHomeNewProduct
     */
    @Override
    public void addProduct(SmsHomeNewProduct smsHomeNewProduct) {
        smsHomeNewProduct.setSort(0);
        smsHomeNewProduct.setRecommendStatus(1);
        smsHomeNewProductMapper.insert(smsHomeNewProduct);
    }
}
